﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.Assert;
using CommunitySystem.Ctrls;
using CommunitySystem.EmpbasicService;
using CommunitySystem.Views;

namespace CommunitySystem.PageView
{
    public partial class BianZhiDetailControl : AsyncCtrl, IReturnListenerc
    {
        #region IReturnListenerc 成员
        public void BeginReturn()
        {

        }
        #endregion
        public NavMoveSwitch Nav { get; set; }
        private EmpbasicService.EmpBasicServiceClient Client = new EmpbasicService.EmpBasicServiceClient();
        EmpbasicService.BianZhiModel RoleModel = new EmpbasicService.BianZhiModel();
        private int OrgID = 0;

        public ObservableCollection<EmpbasicService.BianZhiModel> BianZhiModels =
            new ObservableCollection<EmpbasicService.BianZhiModel>();
        public BianZhiDetailControl()
        {
            InitializeComponent();
            IsWorking.Visibility = Visibility.Collapsed;
            zongbianzhi.AcceptsReturn = true;
            zongbianzhi.TextWrapping = TextWrapping.Wrap;
            DGRole.CanUserResizeColumns = false;
            Client.GetOrgForHRModelCompleted += new EventHandler<EmpbasicService.GetOrgForHRModelCompletedEventArgs>(Client_GetOrgForHRModelCompleted);
            Client.GetOrgForHRModelAsync(0);
            Client.GetBianZhiDetailModelCompleted += new EventHandler<EmpbasicService.GetBianZhiDetailModelCompletedEventArgs>(Client_GetBianZhiDetailModelCompleted);
            Client.UpdateChaoZhiQingKuangCompleted += new EventHandler<UpdateChaoZhiQingKuangCompletedEventArgs>(Client_UpdateChaoZhiQingKuangCompleted);
            Client.GetEmpIdsCompleted += new EventHandler<GetEmpIdsCompletedEventArgs>(Client_GetEmpIdsCompleted);
            Client.GetEmpIds_guoduCompleted += new EventHandler<GetEmpIds_guoduCompletedEventArgs>(Client_GetEmpIds_guoduCompleted);
        }

        void Client_UpdateChaoZhiQingKuangCompleted(object sender, UpdateChaoZhiQingKuangCompletedEventArgs e)
        {
            SilverlightMessageBoxLibrary.Message.InfoMessage("保存成功！");
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if (BianZhiModels != null && BianZhiModels.Count > 0)
                Client.UpdateChaoZhiQingKuangAsync(BianZhiModels[0].OrgID, zongbianzhi.Text);
        }


        void Client_GetBianZhiDetailModelCompleted(object sender, EmpbasicService.GetBianZhiDetailModelCompletedEventArgs e)
        {
            zhengke.Text = fuke.Text = zhuren.Text = fuzhuren.Text = zongbianzhi.Text = "";
            BianZhiModels = e.Result;
            DGRole.ItemsSource = null;
            DGRole.ItemsSource = e.Result;
            if (BianZhiModels != null)
            {
                string zhurenkeyong = "";
                int i1 =
                    Convert.ToInt32(
                        !string.IsNullOrEmpty(BianZhiModels.OrderBy(p => p.FuKeLingDao).FirstOrDefault().FuKeLingDao)
                            ? BianZhiModels.OrderBy(p => p.FuKeLingDao).FirstOrDefault().FuKeLingDao
                            : "0");
                int i2 =
                    Convert.ToInt32(
                        !string.IsNullOrEmpty(
                            BianZhiModels.OrderByDescending(p => p.zhurenshiyou).FirstOrDefault().zhurenshiyou)
                            ? BianZhiModels.OrderByDescending(p => p.zhurenshiyou).FirstOrDefault().zhurenshiyou
                            : "0");

                int i3 = 0;
                foreach (BianZhiModel b in BianZhiModels)
                {
                    if (!string.IsNullOrEmpty(b.ZhuRenJieYongQingKuang) && b.ZhuRenJieYongQingKuang != "无")
                    {
                        if (Math.Abs(Convert.ToInt32(b.ZhuRenJieYongQingKuang)) > i3)
                            i3 = Convert.ToInt32(b.ZhuRenJieYongQingKuang);
                    }
                }
                int i4 =
                    Convert.ToInt32(
                        BianZhiModels.OrderByDescending(p => p.ZhuRenChaoZhiQingKuang).FirstOrDefault().
                            ZhuRenChaoZhiQingKuang == "无"
                            ? "0"
                            : BianZhiModels.OrderByDescending(p => p.ZhuRenChaoZhiQingKuang).FirstOrDefault().
                                  ZhuRenChaoZhiQingKuang);
                zhurenkeyong = "统筹111：" + (i1 - i2 + i3 + i4).ToString();

                string fuzhurenkeyong = "";
                int f1 =
                    Convert.ToInt32(
                        !string.IsNullOrEmpty(BianZhiModels.OrderBy(p => p.FuKeFeiLingDao).FirstOrDefault().FuKeFeiLingDao)
                            ? BianZhiModels.OrderBy(p => p.FuKeFeiLingDao).FirstOrDefault().FuKeFeiLingDao
                            : "0");
                int f2 =
                    Convert.ToInt32(
                        !string.IsNullOrEmpty(
                            BianZhiModels.OrderByDescending(p => p.fuzhurenshiyou).FirstOrDefault().fuzhurenshiyou)
                            ? BianZhiModels.OrderByDescending(p => p.fuzhurenshiyou).FirstOrDefault().fuzhurenshiyou
                            : "0");

                int f3 = 0;
                foreach (BianZhiModel b in BianZhiModels)
                {
                    if (!string.IsNullOrEmpty(b.FuZhuRenJieYongQingKuang) && b.FuZhuRenJieYongQingKuang != "无")
                    {
                        if (Math.Abs(Convert.ToInt32(b.FuZhuRenJieYongQingKuang)) > f3)
                            f3 = Convert.ToInt32(b.FuZhuRenJieYongQingKuang);
                    }
                }
                int f4 =
                    Convert.ToInt32(
                        BianZhiModels.OrderByDescending(p => p.FuZhuRenChaoZhiQingKuang).FirstOrDefault().
                            FuZhuRenChaoZhiQingKuang == "无"
                            ? "0"
                            : BianZhiModels.OrderByDescending(p => p.FuZhuRenChaoZhiQingKuang).FirstOrDefault().
                                  FuZhuRenChaoZhiQingKuang);
                fuzhurenkeyong = "统筹：" + (f1 - f2 + f3 + f4).ToString();

                //if (BianZhiModels.Where(p => p.ZhengKeLingDaoTC == true).Count() > 0)
                //{
                    foreach (BianZhiModel i in BianZhiModels.Where(p => p.ZhengKeLingDaoTC == true))
                    {
                        int zhiwhu = !string.IsNullOrEmpty(i.ZhengKeLingDao) ? Convert.ToInt32(i.ZhengKeLingDao) : 0;
                        int renshu = BianZhiModels.Where(p => p.ZhengKeLingDaoTC == true).Sum(
                            p => !string.IsNullOrEmpty(p.zhengkeshiyou) ? Convert.ToInt32(p.zhengkeshiyou) : 0);
                        int jieyong = BianZhiModels.Where(p => p.ZhengKeLingDaoTC == true).Sum(
                            p =>
                            p.ZhengKeJieYongQingKuang != "无" ? Convert.ToInt32(p.ZhengKeJieYongQingKuang) : 0);
                        int chaozhi = BianZhiModels.Where(p => p.ZhengKeLingDaoTC == true).Sum(
                            p =>
                            p.ZhengKeChaoZhiQingKuang != "无" ? Convert.ToInt32(p.ZhengKeChaoZhiQingKuang) : 0);
                        int jianzhi = BianZhiModels.Where(p => p.ZhengKeLingDaoTC == true).Sum(
                            p => !string.IsNullOrEmpty(p.zhengkejianren) ? Convert.ToInt32(p.zhengkejianren) : 0);
                        i.ZhengKeShiJi =
                            (zhiwhu - renshu + jieyong + chaozhi - jianzhi)
                                .ToString();
                        //i.ZhengKeShiJi =
                        //    (Convert.ToInt32(
                        //        BianZhiModels.Where(p => p.ZhengKeLingDaoTC == true).OrderBy(p => p.ZhengKeShiJi).
                        //            FirstOrDefault().ZhengKeShiJi) +
                        //     BianZhiModels.Sum(
                        //         p => p.ZhengKeJieYongQingKuang != "无" ? Convert.ToInt32(p.ZhengKeJieYongQingKuang) : 0) +
                        //     BianZhiModels.Sum(
                        //         p => p.ZhengKeChaoZhiQingKuang != "无" ? Convert.ToInt32(p.ZhengKeChaoZhiQingKuang) : 0))
                        //        .ToString();
                    }
                    foreach (BianZhiModel i in BianZhiModels.Where(p => p.ZhengKeLingDaoTC == false))
                    {
                        int zhiwhu = !string.IsNullOrEmpty(i.ZhengKeLingDao) ? Convert.ToInt32(i.ZhengKeLingDao) : 0;
                        int renshu = !string.IsNullOrEmpty(i.zhengkeshiyou) ? Convert.ToInt32(i.zhengkeshiyou) : 0;
                        int jieyong = i.ZhengKeJieYongQingKuang != "无" ? Convert.ToInt32(i.ZhengKeJieYongQingKuang) : 0;
                        int chaozhi = i.ZhengKeChaoZhiQingKuang != "无" ? Convert.ToInt32(i.ZhengKeChaoZhiQingKuang) : 0;
                        int jianzhi = !string.IsNullOrEmpty(i.zhengkejianren) ? Convert.ToInt32(i.zhengkejianren) : 0;
                        //int jieyong = BianZhiModels.Where(p => p.ZhengKeLingDaoTC == false).Sum(
                        //    p =>
                        //    p.ZhengKeJieYongQingKuang != "无" ? Convert.ToInt32(p.ZhengKeJieYongQingKuang) : 0);
                        //int chaozhi = BianZhiModels.Where(p => p.ZhengKeLingDaoTC == false).Sum(
                        //    p =>
                        //    p.ZhengKeChaoZhiQingKuang != "无" ? Convert.ToInt32(p.ZhengKeChaoZhiQingKuang) : 0);
                        //int jianzhi = BianZhiModels.Where(p => p.ZhengKeLingDaoTC == true).Sum(
                        //    p => !string.IsNullOrEmpty(p.zhengkejianren) ? Convert.ToInt32(p.zhengkejianren) : 0);
                        i.ZhengKeShiJi =
                            (zhiwhu - renshu + jieyong + chaozhi - jianzhi)
                                .ToString();
                        //i.ZhengKeShiJi =
                        //    (Convert.ToInt32(
                        //        BianZhiModels.Where(p => p.ZhengKeLingDaoTC == true).OrderBy(p => p.ZhengKeShiJi).
                        //            FirstOrDefault().ZhengKeShiJi) +
                        //     BianZhiModels.Sum(
                        //         p => p.ZhengKeJieYongQingKuang != "无" ? Convert.ToInt32(p.ZhengKeJieYongQingKuang) : 0) +
                        //     BianZhiModels.Sum(
                        //         p => p.ZhengKeChaoZhiQingKuang != "无" ? Convert.ToInt32(p.ZhengKeChaoZhiQingKuang) : 0))
                        //        .ToString();
                    }
                //}

                //if (BianZhiModels.Where(p => p.ZhengKeFeiLingDaoTC == true).Count() > 0)
                //{
                    //foreach (BianZhiModel i in BianZhiModels.Where(p => p.ZhengKeFeiLingDaoTC == true))
                    //{
                    //    i.FuKeShiJi =
                    //        BianZhiModels.Where(p => p.ZhengKeFeiLingDaoTC == true).OrderBy(p => p.FuKeShiJi).
                    //            FirstOrDefault().FuKeShiJi;
                    //}
                    foreach (BianZhiModel i in BianZhiModels.Where(p => p.ZhengKeFeiLingDaoTC == true))
                    {
                        i.FuKeShiJi =
                            (Convert.ToInt32(
                                BianZhiModels.Where(p => p.ZhengKeFeiLingDaoTC == true).OrderBy(p => p.FuKeShiJi).
                                    FirstOrDefault().FuKeShiJi) +
                             BianZhiModels.Sum(
                                 p => p.FuKeJieYongQingKuang != "无" ? Convert.ToInt32(p.FuKeJieYongQingKuang) : 0) +
                             BianZhiModels.Sum(
                                 p => p.FuKeChaoZhiQingKuang != "无" ? Convert.ToInt32(p.FuKeChaoZhiQingKuang) : 0))
                                .ToString();

                        int zhiwhu = !string.IsNullOrEmpty(i.ZhengKeFeiLingDao) ? Convert.ToInt32(i.ZhengKeFeiLingDao) : 0;
                        int renshu = BianZhiModels.Where(p => p.ZhengKeFeiLingDaoTC == true).Sum(
                            p => !string.IsNullOrEmpty(p.fukeshiyou) ? Convert.ToInt32(p.fukeshiyou) : 0);
                        int jieyong = BianZhiModels.Where(p => p.ZhengKeFeiLingDaoTC == true).Sum(
                            p =>
                            p.FuKeJieYongQingKuang != "无" ? Convert.ToInt32(p.FuKeJieYongQingKuang) : 0);
                        int chaozhi = BianZhiModels.Where(p => p.ZhengKeFeiLingDaoTC == true).Sum(
                            p =>
                            p.FuKeChaoZhiQingKuang != "无" ? Convert.ToInt32(p.FuKeChaoZhiQingKuang) : 0);
                        int jianzhi = BianZhiModels.Where(p => p.ZhengKeFeiLingDaoTC == true).Sum(
                            p => !string.IsNullOrEmpty(p.fukejianren) ? Convert.ToInt32(p.fukejianren) : 0);
                        i.FuKeShiJi =
                            (zhiwhu - renshu + jieyong + chaozhi - jianzhi)
                                .ToString();
                    }
                    foreach (BianZhiModel i in BianZhiModels.Where(p => p.ZhengKeFeiLingDaoTC == false))
                    {

                        i.FuKeShiJi =
                            (Convert.ToInt32(
                                BianZhiModels.Where(p => p.ZhengKeFeiLingDaoTC == true).OrderBy(p => p.FuKeShiJi).
                                    FirstOrDefault().FuKeShiJi) +
                             BianZhiModels.Sum(
                                 p => p.FuKeJieYongQingKuang != "无" ? Convert.ToInt32(p.FuKeJieYongQingKuang) : 0) +
                             BianZhiModels.Sum(
                                 p => p.FuKeChaoZhiQingKuang != "无" ? Convert.ToInt32(p.FuKeChaoZhiQingKuang) : 0))
                                .ToString();
                        int zhiwhu = !string.IsNullOrEmpty(i.ZhengKeFeiLingDao) ? Convert.ToInt32(i.ZhengKeFeiLingDao) : 0;
                        int renshu = !string.IsNullOrEmpty(i.fukeshiyou) ? Convert.ToInt32(i.fukeshiyou) : 0;
                        int jieyong = i.FuKeJieYongQingKuang != "无" ? Convert.ToInt32(i.FuKeJieYongQingKuang) : 0;
                        int chaozhi = i.FuKeChaoZhiQingKuang != "无" ? Convert.ToInt32(i.FuKeChaoZhiQingKuang) : 0;
                        int jianzhi = !string.IsNullOrEmpty(i.fukejianren) ? Convert.ToInt32(i.fukejianren) : 0;


                        

                        //int zhiwhu = !string.IsNullOrEmpty(i.ZhengKeFeiLingDao) ? Convert.ToInt32(i.ZhengKeFeiLingDao) : 0;
                        //int renshu = BianZhiModels.Where(p => p.ZhengKeFeiLingDaoTC == true).Sum(
                        //    p => !string.IsNullOrEmpty(p.fukeshiyou) ? Convert.ToInt32(p.fukeshiyou) : 0);
                        //int jieyong = BianZhiModels.Where(p => p.ZhengKeFeiLingDaoTC == true).Sum(
                        //    p =>
                        //    p.FuKeJieYongQingKuang != "无" ? Convert.ToInt32(p.FuKeJieYongQingKuang) : 0);
                        //int chaozhi = BianZhiModels.Where(p => p.ZhengKeFeiLingDaoTC == true).Sum(
                        //    p =>
                        //    p.FuKeChaoZhiQingKuang != "无" ? Convert.ToInt32(p.FuKeChaoZhiQingKuang) : 0);

                        i.FuKeShiJi =
                            (zhiwhu - renshu + jieyong + chaozhi - jianzhi)
                                .ToString();
                    }
                //}
                #region 不用了
                foreach (BianZhiModel i in BianZhiModels)
                {
                    i.ZhuRenShiJi = zhurenkeyong;
                    i.FuZhuRenShiJi = fuzhurenkeyong;
                    if (i.JiGouMingCheng == "合计")
                    {
                        zhengke.Text =
                            (-(Convert.ToInt32(!string.IsNullOrEmpty(i.ZhengKeLingDao) ? i.ZhengKeLingDao : "0") -
                            Convert.ToInt32(!string.IsNullOrEmpty(i.zhengkeshiyou) ? i.zhengkeshiyou : "0") -
                            Convert.ToInt32(!string.IsNullOrEmpty(i.zhengkejianren) ? i.zhengkejianren : "0") +
                            Convert.ToInt32(i.ZhengKeJieYongQingKuang != "无" ? i.ZhengKeJieYongQingKuang : "0"))).ToString();
                        if ((-(Convert.ToInt32(!string.IsNullOrEmpty(i.ZhengKeLingDao) ? i.ZhengKeLingDao : "0") -
                            Convert.ToInt32(!string.IsNullOrEmpty(i.zhengkeshiyou) ? i.zhengkeshiyou : "0") -
                            Convert.ToInt32(!string.IsNullOrEmpty(i.zhengkejianren) ? i.zhengkejianren : "0") +
                            Convert.ToInt32(i.ZhengKeJieYongQingKuang != "无" ? i.ZhengKeJieYongQingKuang : "0"))) > 0)
                        {
                            zhengke.Foreground = new SolidColorBrush(Colors.Red);
                        }
                        else
                        {
                            zhengke.Foreground = new SolidColorBrush(Colors.Black);
                        }
                        fuke.Text =
                            (-(Convert.ToInt32(!string.IsNullOrEmpty(i.ZhengKeFeiLingDao) ? i.ZhengKeFeiLingDao : "0") -
                            Convert.ToInt32(!string.IsNullOrEmpty(i.fukeshiyou) ? i.fukeshiyou : "0") -
                            Convert.ToInt32(!string.IsNullOrEmpty(i.fukejianren) ? i.fukejianren : "0") +
                            Convert.ToInt32(i.FuKeJieYongQingKuang != "无" ? i.FuKeJieYongQingKuang : "0"))).ToString();
                        if ((-(Convert.ToInt32(!string.IsNullOrEmpty(i.ZhengKeFeiLingDao) ? i.ZhengKeFeiLingDao : "0") -
                            Convert.ToInt32(!string.IsNullOrEmpty(i.fukeshiyou) ? i.fukeshiyou : "0") -
                            Convert.ToInt32(!string.IsNullOrEmpty(i.fukejianren) ? i.fukejianren : "0") +
                            Convert.ToInt32(i.FuKeJieYongQingKuang != "无" ? i.FuKeJieYongQingKuang : "0"))) > 0)
                        {
                            fuke.Foreground = new SolidColorBrush(Colors.Red);
                        }
                        else
                        {
                            fuke.Foreground = new SolidColorBrush(Colors.Black);
                        }
                        zhuren.Text =
                            (-(Convert.ToInt32(!string.IsNullOrEmpty(i.FuKeLingDao) ? i.FuKeLingDao : "0") -
                            Convert.ToInt32(!string.IsNullOrEmpty(i.zhurenshiyou) ? i.zhurenshiyou : "0") +
                            Convert.ToInt32(i.ZhuRenJieYongQingKuang != "无" ? i.ZhuRenJieYongQingKuang : "0"))).ToString();
                        if ((-(Convert.ToInt32(!string.IsNullOrEmpty(i.FuKeLingDao) ? i.FuKeLingDao : "0") -
                            Convert.ToInt32(!string.IsNullOrEmpty(i.zhurenshiyou) ? i.zhurenshiyou : "0") +
                            Convert.ToInt32(i.ZhuRenJieYongQingKuang != "无" ? i.ZhuRenJieYongQingKuang : "0"))) > 0)
                        {
                            zhuren.Foreground = new SolidColorBrush(Colors.Red);
                        }
                        else
                        {
                            zhuren.Foreground = new SolidColorBrush(Colors.Black);
                        }
                        fuzhuren.Text =
                            (-(Convert.ToInt32(!string.IsNullOrEmpty(i.FuKeFeiLingDao) ? i.FuKeFeiLingDao : "0") -
                            Convert.ToInt32(!string.IsNullOrEmpty(i.fuzhurenshiyou) ? i.fuzhurenshiyou : "0") +
                            Convert.ToInt32(i.FuZhuRenJieYongQingKuang != "无" ? i.FuZhuRenJieYongQingKuang : "0"))).ToString();
                        if ((-(Convert.ToInt32(!string.IsNullOrEmpty(i.FuKeFeiLingDao) ? i.FuKeFeiLingDao : "0") -
                            Convert.ToInt32(!string.IsNullOrEmpty(i.fuzhurenshiyou) ? i.fuzhurenshiyou : "0") +
                            Convert.ToInt32(i.FuZhuRenJieYongQingKuang != "无" ? i.FuZhuRenJieYongQingKuang : "0"))) > 0)
                        {
                            fuzhuren.Foreground = new SolidColorBrush(Colors.Red);
                        }
                        else
                        {
                            fuzhuren.Foreground = new SolidColorBrush(Colors.Black);
                        }
                    }
                }
                #endregion
                if (BianZhiModels.Count > 0 && !string.IsNullOrEmpty(BianZhiModels[0].ChaoZhiQingKuang)) zongbianzhi.Text = BianZhiModels[0].ChaoZhiQingKuang;
            }

        }
        #region 组织机构还有左右拖动的条
        #region 左右拖动的条
        private GridLength _lastColLeftLength;
        private GridLength _lastColRightLength;
        private void GridSpliteTo_ToLeft(object sender, EventArgs e)
        {
            //已经到最左边，直接返回
            if (colLeft.Width.Value == 0.0)
            {
                return;
            }

            //
            if (colRight.Width.Value == 0)
            {
                colLeft.Width = _lastColLeftLength;
                colRight.Width = _lastColRightLength;
            }
            else
            {
                _lastColLeftLength = colLeft.Width;
                _lastColRightLength = colRight.Width;

                colLeft.Width = new GridLength(0);
                colRight.Width = new GridLength(150, GridUnitType.Star);
            }
        }

        private void GridSpliteTo_ToRight(object sender, EventArgs e)
        {
            if (colRight.Width.Value == 0)
            {
                return;
            }

            if (colLeft.Width.Value == 0)
            {
                colLeft.Width = _lastColLeftLength;
                colRight.Width = _lastColRightLength;
            }
            else
            {
                _lastColLeftLength = colLeft.Width;
                _lastColRightLength = colRight.Width;

                colRight.Width = new GridLength(0);
                colLeft.Width = new GridLength(300, GridUnitType.Star);
            }
        }
        #endregion
        void Client_GetOrgForHRModelCompleted(object sender, EmpbasicService.GetOrgForHRModelCompletedEventArgs e)
        {
            foreach (EmpbasicService.OrgForHRModel i in e.Result)
            {
                forumList.Add(i);
            }
            //forumList.Add(new EmpbasicService.OrgForHRModel() { ID = 1, Type = "全部单位", Name = "组织机构" });
            //forumList.Add(new EmpbasicService.OrgForHRModel() { ID = 1, Type = "组织机构", Name = "党群系统" });
            //forumList.Add(new EmpbasicService.OrgForHRModel() { ID = 1, Type = "组织机构", Name = "政法系统" });

            //forumList.Add(new EmpbasicService.OrgForHRModel() { ID = 1, Type = "组织机构", Name = "行政系统" });
            //forumList.Add(new EmpbasicService.OrgForHRModel() { ID = 1, Type = "组织机构", Name = "参公系统" });
            //forumList.Add(new EmpbasicService.OrgForHRModel() { ID = 1, Type = "组织机构", Name = "地区系统" });
            AddTreeNode("全部单位", null);
        }
        ObservableCollection<EmpbasicService.OrgForHRModel> forumList = new ObservableCollection<EmpbasicService.OrgForHRModel>();


        private void AddTreeNode(string type, TreeViewItem treeViewItem)
        {
            List<EmpbasicService.OrgForHRModel> result = (from forumInfo in forumList
                                                          where forumInfo.Type == type
                                                          select forumInfo).ToList<EmpbasicService.OrgForHRModel>();

            if (result.Count > 0)
            {
                foreach (EmpbasicService.OrgForHRModel foruminfo in result)
                {
                    TreeViewItem objTreeNode = new TreeViewItem();

                    //StackPanel sp = new StackPanel();
                    CheckBox cb = new CheckBox();
                    cb.Content = foruminfo.Name;
                    cb.Tag = objTreeNode;
                    cb.DataContext = foruminfo;
                    //sp.Children.Add(cb);
                    cb.Click += new RoutedEventHandler(cb_Checked);
                    objTreeNode.Header = cb;

                    //objTreeNode.Header = foruminfo.Name;
                    //objTreeNode.DataContext = foruminfo;
                    //objTreeNode.Tag = foruminfo.ID;
                    if ((objTreeNode.Header as CheckBox).Content.ToString() == "组织机构") objTreeNode.IsExpanded = true;

                    //此样式将会添加的所有叶子结点上
                    //objTreeNode.ItemContainerStyle = this.Resources["RedItemStyle"] as Style;

                    //添加根节点
                    if (treeViewItem == null)
                    {
                        TreeOfLife.Items.Add(objTreeNode);
                    }
                    else
                    {
                        treeViewItem.Items.Add(objTreeNode);
                    }
                    AddTreeNode(foruminfo.Name, objTreeNode);
                }
            }
        }
        void cb_Checked(object sender, RoutedEventArgs e)
        {
            CheckBox cb = sender as CheckBox;
            EmpbasicService.OrgForHRModel rm = cb.DataContext as EmpbasicService.OrgForHRModel;
            if (cb.IsChecked == true)
            {
                if (!orgids.Contains(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString()))
                    orgids.Add(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString());

                CheckChildItem(cb.Tag as TreeViewItem);
            }
            else
            {
                if (orgids != null)
                {
                    if (orgids.Contains(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString()))
                        orgids.Remove(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString());
                }
                UnCheckChildItem(cb.Tag as TreeViewItem);
            }
            LoadData();
        }
        void CheckParentItem(TreeViewItem item)
        {
            TreeViewItem tvi = item.GetParentTreeViewItem();
            if (tvi != null)
            {
                CheckBox cb = tvi.Header as CheckBox;
                cb.IsChecked = true;
                CheckParentItem(tvi);
            }
        }
        public List<string> orgids = new List<string>();
        void CheckChildItem(TreeViewItem item)
        {
            foreach (TreeViewItem tvi in item.Items)
            {
                if (tvi != null)
                {
                    CheckBox cb = tvi.Header as CheckBox;
                    cb.IsChecked = true;
                    if (!orgids.Contains(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString()))
                        orgids.Add(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString());
                    CheckChildItem(tvi);
                }
            }
        }
        void UnCheckChildItem(TreeViewItem item)
        {
            foreach (TreeViewItem tvi in item.Items)
            {
                if (tvi != null)
                {
                    CheckBox cb = tvi.Header as CheckBox;
                    cb.IsChecked = false;
                    if (orgids.Contains(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString()))
                        orgids.Remove(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString());
                    UnCheckChildItem(tvi);
                }
            }
        }
        private void TreeOfLife_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            TreeViewItem item = e.NewValue as TreeViewItem;
            if (item != null)
            {
                if (item.Tag != null && Convert.ToInt32(item.Tag) != 0)
                {
                    OrgID = Convert.ToInt32(item.Tag);
                    LoadData();
                }

            }
        }
        #endregion
        string or = "";
        void LoadData()
        {
            if (orgids.Count > 0)
            {
                if (orgids.Count > 1)
                {
                    SilverlightMessageBoxLibrary.Message.ErrorMessage("只能查看一个单位的编制信息！");
                    return;
                }
                or = orgids[0];
            }
            else or = "";
            Client.GetBianZhiDetailModelAsync(or, "");
        }
        #region 正科
        private void zhengkerenshu_Click(object sender, RoutedEventArgs e)
        {
            HyperlinkButton hbtn = sender as HyperlinkButton;
            if (hbtn != null)
            {
                BianZhiEmpDetailControl view = new BianZhiEmpDetailControl(BianZhiModels[0].OrgID, "正科", hbtn.Tag.ToString(), "在任");
                Nav.ShowNextPage(hbtn.Tag.ToString() + "-人员详细信息", view, this);
            }

        }

        private void zhengkejianren_Click(object sender, RoutedEventArgs e)
        {
            HyperlinkButton hbtn = sender as HyperlinkButton;
            if (hbtn != null)
            {
                BianZhiEmpDetailControl view = new BianZhiEmpDetailControl(BianZhiModels[0].OrgID, "正科", hbtn.Tag.ToString(), "兼任");
                Nav.ShowNextPage(hbtn.Tag.ToString() + "-人员详细信息", view, this);
            }
        }

        private int KeShiID = 0;
        private int ZhengFu = 0;
        private bool IsNew = false;
        private BianZhiModel IsSelectedBianZhi = new BianZhiModel();
        private string Type;
        private void zhengkejieyong_Click(object sender, RoutedEventArgs e)
        {
            Type = "正科";
            HyperlinkButton hbtn = sender as HyperlinkButton;
            if (hbtn != null)
            {
                
                int z =
                    !string.IsNullOrEmpty(
                        BianZhiModels.Where(p => p.ID == Convert.ToInt32(hbtn.Tag)).FirstOrDefault().ZhengKeLingDao)
                        ? Convert.ToInt32(
                            BianZhiModels.Where(p => p.ID == Convert.ToInt32(hbtn.Tag)).FirstOrDefault().
                                ZhengKeLingDao)
                        : 0;
                int z1 = !string.IsNullOrEmpty(
                        BianZhiModels.Where(p => p.ID == Convert.ToInt32(hbtn.Tag)).FirstOrDefault().zhengkeshiyou)
                        ? Convert.ToInt32(
                            BianZhiModels.Where(p => p.ID == Convert.ToInt32(hbtn.Tag)).FirstOrDefault().
                                zhengkeshiyou)
                        : 0;



                if (hbtn.Content.ToString() != "无")
                {
                    KeShiID = Convert.ToInt32(hbtn.Tag);
                    ZhengFu = Convert.ToInt32(hbtn.Content);
                    if (ZhengFu > 0)
                    {
                        BianZhi_Emp_JieYongWindow ejwin = new BianZhi_Emp_JieYongWindow("借用");
                        ejwin.Show();
                        ejwin.Closed += new EventHandler(ejwin_Closed);
                    }
                    else
                    {
                        BianZhiDetail_YiJieYongWindow win = new BianZhiDetail_YiJieYongWindow(Type, KeShiID,
                                                                                          ZhengFu);
                        win.Show();
                    }

                }
                else
                {
                    if (Convert.ToInt32(BianZhiModels.Where(p => p.ID == Convert.ToInt32(hbtn.Tag)).FirstOrDefault().ZhengKeShiJi) > 0)
                    {
                        SilverlightMessageBoxLibrary.Message.ErrorMessage("该科室还有编制可用，不可借用其他科室编制！");
                        return;
                    }
                    //BianZhiDetailWindow win = new BianZhiDetailWindow(or, Type + "借用", Convert.ToInt32(hbtn.Tag),
                    //    BianZhiModels.Where(p => p.ID == Convert.ToInt32(hbtn.Tag)).FirstOrDefault().ZhengKeLingDaoTC);
                    ObservableCollection<EmpbasicService.BianZhiModel> d = new ObservableCollection<BianZhiModel>();
                    foreach (BianZhiModel i in BianZhiModels.Where(p => Convert.ToInt32(p.ZhengKeShiJi) > 0 && p.JiGouMingCheng != "合计"))
                    {
                        
                        d.Add(i);
                    }
                    //IEnumerable<BianZhiModel> d = BianZhiModels.Where(p => Convert.ToInt32(p.ZhengKeShiJi) > 0 && p.JiGouMingCheng != "合计");
                    BianZhiDetailWindow win = new BianZhiDetailWindow(d,null,null,null, Type + "借用", Convert.ToInt32(hbtn.Tag));
                    win.Show();
                    win.Closed += new EventHandler(win_Closed);
                }

            }

        }

        void ejwin_Closed(object sender, EventArgs e)
        {
            if(((BianZhi_Emp_JieYongWindow)sender).DialogResult == true)
            if (((BianZhi_Emp_JieYongWindow)sender).Type == "职位借用情况")
            {
                BianZhiDetail_YiJieYongWindow win = new BianZhiDetail_YiJieYongWindow(Type, KeShiID,
                                                                                          ZhengFu);
                win.Show();
                win.Closed += new EventHandler(win_Closed);
            }
            else
            {
                Client.GetEmpIdsAsync(KeShiID, Type, 0);
            }
        }
        void Client_GetEmpIdsCompleted(object sender, GetEmpIdsCompletedEventArgs e)
        {
            if (e.Result != null && e.Result.Count > 0)
            {
                BianZhiEmpDetailControl view = new BianZhiEmpDetailControl(e.Result, "借用", 1, BianZhiModels[0].OrgID, Type,
                                                                                    KeShiID);
                Nav.ShowNextPage("借用职位-人员详细信息", view, this);
            }
            else
            {
                if (MessageBox.Show("未给职位安排人员，是否现在安排？", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    BianZhi_AnPaiRenYuanWindow win = new BianZhi_AnPaiRenYuanWindow(BianZhiModels[0].OrgID, Type,
                                                                                    KeShiID, 1, "借用",null);
                    win.Show();
                }
            }
        }

        private int RenShu;
        private void zhengkechaozhi_Click(object sender, RoutedEventArgs e)
        {
            Type = "正科";
            HyperlinkButton hbtn = sender as HyperlinkButton;
            IsSelectedBianZhi = DGRole.SelectedItem as BianZhiModel;
            if (hbtn != null && IsSelectedBianZhi != null)
            {
                if (hbtn.Content.ToString() != "无")
                {
                    RenShu = Convert.ToInt32(hbtn.Content);
                    BianZhi_Emp_JieYongWindow ejwin = new BianZhi_Emp_JieYongWindow("超职数");
                    ejwin.Show();
                    ejwin.Closed += new EventHandler(ejwin_Closed1);
                    KeShiID = Convert.ToInt32(hbtn.Tag);
                    ZhengFu = Convert.ToInt32(hbtn.Content);
                    IsNew = hbtn.Content.ToString() == "无" ? true : false;
                }
                else
                {
                    BianZhi_GuoDuXingWindow win = new BianZhi_GuoDuXingWindow(Convert.ToInt32(hbtn.Tag), IsSelectedBianZhi, Type + "借用", hbtn.Content.ToString() == "无" ? true : false);
                    win.Show();
                    win.Closed += new EventHandler(win_Closed);
                }
            }
        }
        void ejwin_Closed1(object sender, EventArgs e)
        {
            if (((BianZhi_Emp_JieYongWindow)sender).DialogResult == true)
                if (((BianZhi_Emp_JieYongWindow)sender).Type == "过渡性超值情况")
                {
                    BianZhi_GuoDuXingWindow win = new BianZhi_GuoDuXingWindow(KeShiID, IsSelectedBianZhi, Type + "借用", IsNew);
                    win.Show();
                    win.Closed += new EventHandler(win_Closed);
                }
                else
                {
                    Client.GetEmpIds_guoduAsync(KeShiID, Type, 1);
                }
        }

        void Client_GetEmpIds_guoduCompleted(object sender, GetEmpIds_guoduCompletedEventArgs e)
        {
            if (e.Result != null && e.Result.Count > 0)
            {
                BianZhiEmpDetailControl view = new BianZhiEmpDetailControl(e.Result, "过渡性", RenShu, BianZhiModels[0].OrgID, Type,
                                                                                    KeShiID);
                Nav.ShowNextPage("借用职位-人员详细信息", view, this);
            }
            else
            {
                if (MessageBox.Show("未给职位安排人员，是否现在安排？", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    BianZhi_AnPaiRenYuanWindow win = new BianZhi_AnPaiRenYuanWindow(BianZhiModels[0].OrgID, Type,
                                                                                    KeShiID, RenShu, "过渡性",null);
                    win.Show();
                }
            }
        }
        #endregion
        void win_Closed(object sender, EventArgs e)
        {
            LoadData();
        }
        private void fukeshiyou_Click(object sender, RoutedEventArgs e)
        {
            HyperlinkButton hbtn = sender as HyperlinkButton;
            if (hbtn != null)
            {
                BianZhiEmpDetailControl view = new BianZhiEmpDetailControl(BianZhiModels[0].OrgID, "副科", hbtn.Tag.ToString(), "在任");
                Nav.ShowNextPage(hbtn.Tag.ToString() + "-人员详细信息", view, this);
            }
        }
        private void fukejianren_Click(object sender, RoutedEventArgs e)
        {
            HyperlinkButton hbtn = sender as HyperlinkButton;
            if (hbtn != null)
            {
                BianZhiEmpDetailControl view = new BianZhiEmpDetailControl(BianZhiModels[0].OrgID, "副科", hbtn.Tag.ToString(), "兼任");
                Nav.ShowNextPage(hbtn.Tag.ToString() + "-人员详细信息", view, this);
            }
        }
        private void fukejieyong_Click(object sender, RoutedEventArgs e)
        {
            Type = "副科";
            HyperlinkButton hbtn = sender as HyperlinkButton;
            if (hbtn != null)
            {
                int z =
                    !string.IsNullOrEmpty(
                        BianZhiModels.Where(p => p.ID == Convert.ToInt32(hbtn.Tag)).FirstOrDefault().ZhengKeFeiLingDao)
                        ? Convert.ToInt32(
                            BianZhiModels.Where(p => p.ID == Convert.ToInt32(hbtn.Tag)).FirstOrDefault().
                                ZhengKeFeiLingDao)
                        : 0;
                int z1 = !string.IsNullOrEmpty(
                        BianZhiModels.Where(p => p.ID == Convert.ToInt32(hbtn.Tag)).FirstOrDefault().fukeshiyou)
                        ? Convert.ToInt32(
                            BianZhiModels.Where(p => p.ID == Convert.ToInt32(hbtn.Tag)).FirstOrDefault().
                                fukeshiyou)
                        : 0;



                if (hbtn.Content.ToString() != "无")
                {
                    KeShiID = Convert.ToInt32(hbtn.Tag);
                    ZhengFu = Convert.ToInt32(hbtn.Content);
                    if (ZhengFu > 0)
                    {
                        BianZhi_Emp_JieYongWindow ejwin = new BianZhi_Emp_JieYongWindow("借用");
                        ejwin.Show();
                        ejwin.Closed += new EventHandler(ejwin_Closed);
                    }
                    else
                    {
                        BianZhiDetail_YiJieYongWindow win = new BianZhiDetail_YiJieYongWindow(Type, KeShiID,
                                                                                          ZhengFu);
                        win.Show();
                    }

                }
                else
                {
                    if (Convert.ToInt32(BianZhiModels.Where(p => p.ID == Convert.ToInt32(hbtn.Tag)).FirstOrDefault().FuKeShiJi) > 0)
                    {
                        SilverlightMessageBoxLibrary.Message.ErrorMessage("该科室还有编制可用，不可借用其他科室编制！");
                        return;
                    }
                    ObservableCollection<EmpbasicService.BianZhiModel> d = new ObservableCollection<BianZhiModel>();
                    ObservableCollection<EmpbasicService.BianZhiModel> d1 = new ObservableCollection<BianZhiModel>();
                    foreach (BianZhiModel i in BianZhiModels.Where(p => Convert.ToInt32(p.ZhengKeShiJi) > 0 && p.JiGouMingCheng != "合计"))
                    {
                        d.Add(i);
                    }
                    foreach (BianZhiModel i in BianZhiModels.Where(p => Convert.ToInt32(p.FuKeShiJi) > 0 && p.JiGouMingCheng != "合计"))
                    {
                        d1.Add(i);
                    }
                    //IEnumerable<BianZhiModel> d1 = BianZhiModels.Where(p => Convert.ToInt32(p.FuKeShiJi) > 0 && p.JiGouMingCheng != "合计");
                    BianZhiDetailWindow win = new BianZhiDetailWindow(d,d1,null,null, Type + "借用", Convert.ToInt32(hbtn.Tag));
                    win.Show();
                    win.Closed += new EventHandler(win_Closed);
                }

            }
        }
        private void fukechaozhi_Click(object sender, RoutedEventArgs e)
        {
            Type = "副科";
            HyperlinkButton hbtn = sender as HyperlinkButton;
            IsSelectedBianZhi = DGRole.SelectedItem as BianZhiModel;
            if (hbtn != null && IsSelectedBianZhi != null)
            {
                if (hbtn.Content.ToString() != "无")
                {
                    RenShu = Convert.ToInt32(hbtn.Content);
                    BianZhi_Emp_JieYongWindow ejwin = new BianZhi_Emp_JieYongWindow("超职数");
                    ejwin.Show();
                    ejwin.Closed += new EventHandler(ejwin_Closed1);
                    KeShiID = Convert.ToInt32(hbtn.Tag);
                    ZhengFu = Convert.ToInt32(hbtn.Content);
                    IsNew = hbtn.Content.ToString() == "无" ? true : false;
                }
                else
                {
                    BianZhi_GuoDuXingWindow win = new BianZhi_GuoDuXingWindow(Convert.ToInt32(hbtn.Tag), IsSelectedBianZhi, Type + "借用", hbtn.Content.ToString() == "无" ? true : false);
                    win.Show();
                    win.Closed += new EventHandler(win_Closed);
                }
            }

        }
        private void zhurenshiyou_Click(object sender, RoutedEventArgs e)
        {
            HyperlinkButton hbtn = sender as HyperlinkButton;
            if (hbtn != null)
            {
                BianZhiEmpDetailControl view = new BianZhiEmpDetailControl(BianZhiModels[0].OrgID, "主任", hbtn.Tag.ToString(), "在任");
                Nav.ShowNextPage(hbtn.Tag.ToString() + "-人员详细信息", view, this);
            }
        }
        private void zhurenjieyong_Click(object sender, RoutedEventArgs e)
        {
            Type = "主任";
            HyperlinkButton hbtn = sender as HyperlinkButton;
            BianZhiModel model = BianZhiModels.Where(p => p.ID == Convert.ToInt32(hbtn.Tag)).FirstOrDefault();
            if (hbtn != null)
            {

                if (hbtn.Content.ToString() != "无")
                {
                    KeShiID = Convert.ToInt32(hbtn.Tag);
                    ZhengFu = Convert.ToInt32(hbtn.Content);
                    if (ZhengFu > 0)
                    {
                        BianZhi_Emp_JieYongWindow ejwin = new BianZhi_Emp_JieYongWindow("借用");
                        ejwin.Show();
                        ejwin.Closed += new EventHandler(ejwin_Closed);
                    }
                    else
                    {
                        BianZhiDetail_YiJieYongWindow win = new BianZhiDetail_YiJieYongWindow(Type, KeShiID,
                                                                                          ZhengFu);
                        win.Show();
                    }

                }
                else
                {
                    if (model.ZhuRenShiJi.Split('：').Count() > 0 && !string.IsNullOrEmpty(model.ZhuRenShiJi.Split('：')[1]))
                    {
                        if (Convert.ToInt32(model.ZhuRenShiJi.Split('：')[1]) > 0)
                        {
                            SilverlightMessageBoxLibrary.Message.ErrorMessage("该科室还有编制可用，不可借用其他科室编制！");
                            return;
                        }
                        ObservableCollection<EmpbasicService.BianZhiModel> d = new ObservableCollection<BianZhiModel>();
                        foreach (BianZhiModel i in BianZhiModels.Where(p => Convert.ToInt32(p.ZhengKeShiJi) > 0 && p.JiGouMingCheng != "合计"))
                        {
                            d.Add(i);
                        }
                        //IEnumerable<BianZhiModel> d1 = BianZhiModels.Where(p => Convert.ToInt32(p.FuKeShiJi) > 0 && p.JiGouMingCheng != "合计");
                        BianZhiDetailWindow win = new BianZhiDetailWindow(d, null, null, null, Type + "借用", Convert.ToInt32(hbtn.Tag));
                        win.Show();
                        win.Closed += new EventHandler(win_Closed);
                    }
                    
                }

            }
        }
        private void zhurenchaozhi_Click(object sender, RoutedEventArgs e)
        {
            Type = "主任";
            HyperlinkButton hbtn = sender as HyperlinkButton;
            IsSelectedBianZhi = DGRole.SelectedItem as BianZhiModel;
            if (hbtn != null && IsSelectedBianZhi != null)
            {
                if (hbtn.Content.ToString() != "无")
                {
                    RenShu = Convert.ToInt32(hbtn.Content);
                    BianZhi_Emp_JieYongWindow ejwin = new BianZhi_Emp_JieYongWindow("超职数");
                    ejwin.Show();
                    ejwin.Closed += new EventHandler(ejwin_Closed1);
                    KeShiID = Convert.ToInt32(hbtn.Tag);
                    ZhengFu = Convert.ToInt32(hbtn.Content);
                    IsNew = hbtn.Content.ToString() == "无" ? true : false;
                }
                else
                {
                    BianZhi_GuoDuXingWindow win = new BianZhi_GuoDuXingWindow(Convert.ToInt32(hbtn.Tag), IsSelectedBianZhi, Type + "借用", hbtn.Content.ToString() == "无" ? true : false);
                    win.Show();
                    win.Closed += new EventHandler(win_Closed);
                }
            }

        }
        private void fuzhurenshiyou_Click(object sender, RoutedEventArgs e)
        {
            HyperlinkButton hbtn = sender as HyperlinkButton;
            if (hbtn != null)
            {
                BianZhiEmpDetailControl view = new BianZhiEmpDetailControl(BianZhiModels[0].OrgID, "副主任", hbtn.Tag.ToString(), "在任");
                Nav.ShowNextPage(hbtn.Tag.ToString() + "-人员详细信息", view, this);
            }
        }
        private void fuzhurenjieyong_Click(object sender, RoutedEventArgs e)
        {

            Type = "副主任";
            HyperlinkButton hbtn = sender as HyperlinkButton;
            BianZhiModel model = BianZhiModels.Where(p => p.ID == Convert.ToInt32(hbtn.Tag)).FirstOrDefault();
            if (hbtn != null)
            {

                if (hbtn.Content.ToString() != "无")
                {
                    KeShiID = Convert.ToInt32(hbtn.Tag);
                    ZhengFu = Convert.ToInt32(hbtn.Content);
                    if (ZhengFu > 0)
                    {
                        BianZhi_Emp_JieYongWindow ejwin = new BianZhi_Emp_JieYongWindow("借用");
                        ejwin.Show();
                        ejwin.Closed += new EventHandler(ejwin_Closed);
                    }
                    else
                    {
                        BianZhiDetail_YiJieYongWindow win = new BianZhiDetail_YiJieYongWindow(Type, KeShiID,
                                                                                          ZhengFu);
                        win.Show();
                    }

                }
                else
                {
                    if (model.FuZhuRenShiJi != "统筹：0")
                    {
                        SilverlightMessageBoxLibrary.Message.ErrorMessage("该科室还有编制可用，不可借用其他科室编制！");
                        return;
                    }
                    ObservableCollection<EmpbasicService.BianZhiModel> d = new ObservableCollection<BianZhiModel>();
                    ObservableCollection<EmpbasicService.BianZhiModel> d1 = new ObservableCollection<BianZhiModel>();
                    ObservableCollection<EmpbasicService.BianZhiModel> d2 = new ObservableCollection<BianZhiModel>();
                    foreach (BianZhiModel i in BianZhiModels.Where(p => Convert.ToInt32(p.ZhengKeShiJi) > 0 && p.JiGouMingCheng != "合计"))
                    {
                        d.Add(i);
                    }
                    foreach (BianZhiModel i in BianZhiModels.Where(p => Convert.ToInt32(p.FuKeShiJi) > 0 && p.JiGouMingCheng != "合计"))
                    {
                        d1.Add(i);
                    }
                    foreach (BianZhiModel i in BianZhiModels.Where(p => p.ZhuRenShiJi != "统筹：0" && p.JiGouMingCheng != "合计"))
                    {
                        d2.Add(i);
                    }
                    //IEnumerable<BianZhiModel> d1 = BianZhiModels.Where(p => Convert.ToInt32(p.FuKeShiJi) > 0 && p.JiGouMingCheng != "合计");
                    BianZhiDetailWindow win = new BianZhiDetailWindow(d, d1, d2, null, Type + "借用", Convert.ToInt32(hbtn.Tag));
                    win.Show();
                    win.Closed += new EventHandler(win_Closed);
                }

            }
        }
        private void fuzhurenchaozhi_Click(object sender, RoutedEventArgs e)
        {
            Type = "副主任";
            HyperlinkButton hbtn = sender as HyperlinkButton;
            IsSelectedBianZhi = DGRole.SelectedItem as BianZhiModel;
            if (hbtn != null && IsSelectedBianZhi != null)
            {
                if (hbtn.Content.ToString() != "无")
                {
                    RenShu = Convert.ToInt32(hbtn.Content);
                    BianZhi_Emp_JieYongWindow ejwin = new BianZhi_Emp_JieYongWindow("超职数");
                    ejwin.Show();
                    ejwin.Closed += new EventHandler(ejwin_Closed1);
                    KeShiID = Convert.ToInt32(hbtn.Tag);
                    ZhengFu = Convert.ToInt32(hbtn.Content);
                    IsNew = hbtn.Content.ToString() == "无" ? true : false;
                }
                else
                {
                    BianZhi_GuoDuXingWindow win = new BianZhi_GuoDuXingWindow(Convert.ToInt32(hbtn.Tag), IsSelectedBianZhi, Type + "借用", hbtn.Content.ToString() == "无" ? true : false);
                    win.Show();
                    win.Closed += new EventHandler(win_Closed);
                }
            }

        }
        private void keyuanshiyou_Click(object sender, RoutedEventArgs e)
        {
            HyperlinkButton hbtn = sender as HyperlinkButton;
            if (hbtn != null)
            {
                BianZhiEmpDetailControl view = new BianZhiEmpDetailControl(BianZhiModels[0].OrgID, "科员", hbtn.Tag.ToString(), "在任");
                Nav.ShowNextPage(hbtn.Tag.ToString() + "-人员详细信息", view, this);
            }
        }
        private Stream _Stream = null;
        private void btnOut_Click(object sender, RoutedEventArgs e)
        {
            if(BianZhiModels.Count <= 0)
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("无数据可导出！");
                return;
            }
            SaveFileDialog dlg = new SaveFileDialog()
            {
                Filter = "Excel文件 (*.xls)|*.xls"
            };

            if (!(bool)dlg.ShowDialog())
            {
                return;
            }
            try
            {
                IsWorking.Visibility = Visibility.Visible;
                busy.BusyContent = "正在导出数据，请稍后...";
                _Stream = dlg.OpenFile();
            }
            catch
            {
                MessageBox.Show("其它程序正在使用该文件，请关闭使用该文件的程序后重试！");
                return;
            }
            Client.ExportDW1Completed += new EventHandler<ExportDW1CompletedEventArgs>(Client_ExportDW1Completed);
            Client.ExportDW1Async(or);
            //Client.ExportDW1Async(or);
            //Client.ExportDW1Completed += (ss, ee) =>
            //{

            //    try
            //    {
            //        byte[] buffer = ee.Result;
            //        _Stream.Write(buffer, 0, buffer.Length);
            //        IsWorking.Visibility = Visibility.Collapsed;
            //        MessageBox.Show("导出完毕！");
            //        _Stream.Close();
            //        _Stream.Dispose();
            //        return;
            //    }
            //    catch
            //    {

            //    }
            //};
        }

        void Client_ExportDW1Completed(object sender, ExportDW1CompletedEventArgs e)
        {
           try
           {
               byte[] buffer = e.Result;
               _Stream.Write(buffer, 0, buffer.Length);
              
               MessageBox.Show("导出完毕！");
               IsWorking.Visibility = Visibility.Collapsed;
               _Stream.Close();
               _Stream.Dispose();
               return;
           }
           catch
           {

           }
        }


    }
}
